cmake_minimum_required(VERSION 3.2)

PROJECT(luapb)
LIST(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
INCLUDE(cmake/ModuleImport.cmake)
INCLUDE(cmake/ModuleCompileOptions.cmake)
ModuleSetCompileOptions()

ModuleImport("dmprotobuf" "thirdparty/dmprotobuf")
ModuleImport("dmyaml" "thirdparty/dmyaml")
ModuleImport("luajson" "thirdparty/luajson")

OPTION(USE_LUAPB_INNER "use lua inner" ON)
OPTION(USE_LUAPB_TEST "use luapb test" ON)

SET(LUAPB_VERSION_MAJOR "1")
SET(LUAPB_VERSION_MINOR "0")
SET(LUAPB_VERSION_PATCH "1")
SET(LUAPB_VERSION "${LUAPB_VERSION_MAJOR}.${LUAPB_VERSION_MINOR}.${LUAPB_VERSION_PATCH}")

MESSAGE(STATUS "VERSION: ${LUAPB_VERSION}")

ADD_DEFINITIONS(-DUSE_LUAPB)
ADD_DEFINITIONS(-DLUA_COMPAT_MODULE)

option(USE_INTERNAL_FPCONV "Use internal strtod() / g_fmt() code for performance" ON)

IF(MSVC)
    ADD_DEFINITIONS(-Dstrncasecmp=_strnicmp)
ENDIF()

#ModuleImport("dmlog" "thirdparty/dmlog")

if(USE_INTERNAL_FPCONV)
    # Use libc number conversion routines (strtod(), sprintf())
    add_definitions(-DUSE_INTERNAL_FPCONV)
endif()

IF (WIN32)
    INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/src/windows)
ENDIF(WIN32)

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/include)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/src)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/src/lua)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/src/tolua)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/src/luapb)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/src/luajson)

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/test)

FILE(GLOB DMLUA_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/lua/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/lua/*.cc
${CMAKE_CURRENT_SOURCE_DIR}/src/lua/*.c
${CMAKE_CURRENT_SOURCE_DIR}/src/lua/*.hpp
${CMAKE_CURRENT_SOURCE_DIR}/src/lua/*.h

)

LIST(FILTER DMLUA_SOURCES EXCLUDE REGEX "lua.c$")
LIST(FILTER DMLUA_SOURCES EXCLUDE REGEX "luac.c$")
LIST(FILTER DMLUA_SOURCES EXCLUDE REGEX "wmain.c$")

FILE(GLOB LUA_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/lua/lua.c
)

FILE(GLOB LUAPB_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/include/*.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/*.h

${CMAKE_CURRENT_SOURCE_DIR}/src/luapb/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/luapb/*.cc
${CMAKE_CURRENT_SOURCE_DIR}/src/luapb/*.c
${CMAKE_CURRENT_SOURCE_DIR}/src/luapb/*.hpp
${CMAKE_CURRENT_SOURCE_DIR}/src/luapb/*.h

${CMAKE_CURRENT_SOURCE_DIR}/src/luajson/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/luajson/*.cc
${CMAKE_CURRENT_SOURCE_DIR}/src/luajson/*.c
${CMAKE_CURRENT_SOURCE_DIR}/src/luajson/*.hpp
${CMAKE_CURRENT_SOURCE_DIR}/src/luajson/*.h

${CMAKE_CURRENT_SOURCE_DIR}/src/tolua/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/tolua/*.cc
${CMAKE_CURRENT_SOURCE_DIR}/src/tolua/*.c
${CMAKE_CURRENT_SOURCE_DIR}/src/tolua/*.hpp
${CMAKE_CURRENT_SOURCE_DIR}/src/tolua/*.h
)

IF (WIN32)
    LIST(APPEND LUAPB_SOURCES)
ENDIF(WIN32)

FILE(GLOB_RECURSE LUAPBTEST_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/test/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/*.cc
${CMAKE_CURRENT_SOURCE_DIR}/test/*.c
${CMAKE_CURRENT_SOURCE_DIR}/test/*.hpp
${CMAKE_CURRENT_SOURCE_DIR}/test/*.h)

IF (WIN32)
    IF (USE_LUAPB_INNER)
        ADD_LIBRARY(lua53 SHARED ${DMLUA_SOURCES})
        ADD_EXECUTABLE(lua ${LUA_SOURCES})
        TARGET_LINK_LIBRARIES(lua lua53)
        SET_TARGET_PROPERTIES(lua53 PROPERTIES COMPILE_FLAGS "-DLUA_BUILD_AS_DLL -DLUA_CORE")
    ENDIF(USE_LUAPB_INNER)
    
    ADD_LIBRARY(luapb SHARED ${LUAPB_SOURCES} ${CMAKE_CURRENT_SOURCE_DIR}/src/luapb/luapb_module.def)
    TARGET_LINK_LIBRARIES(luapb lua53 dmyaml libprotobuf libprotoc)
    SET_TARGET_PROPERTIES(luapb PROPERTIES COMPILE_FLAGS "-DLUA_BUILD_AS_DLL -DLUA_LIB")

    IF (USE_LUAPB_TEST)
        ADD_EXECUTABLE(luapbtest ${LUAPBTEST_SOURCES})
        TARGET_LINK_LIBRARIES(luapbtest luapb lua53 libprotobuf libprotoc)
        SET_TARGET_PROPERTIES(luapbtest PROPERTIES COMPILE_FLAGS "-DLUA_BUILD_AS_DLL")
    ENDIF(USE_LUAPB_TEST)
ELSEIF (APPLE)
    ADD_DEFINITIONS(-DLUA_USE_MACOSX)

    IF (USE_LUAPB_INNER)
        ADD_LIBRARY(lua53 SHARED ${DMLUA_SOURCES})
        ADD_EXECUTABLE(lua ${LUA_SOURCES})
        TARGET_LINK_LIBRARIES(lua lua53 dl)
        SET_TARGET_PROPERTIES(lua53 PROPERTIES COMPILE_FLAGS "-Wl,-E")
    ENDIF(USE_LUAPB_INNER)

    ADD_LIBRARY(luapb SHARED ${LUAPB_SOURCES})
    SET_TARGET_PROPERTIES(luapb PROPERTIES COMPILE_FLAGS "-Wl,-E" )
    SET_TARGET_PROPERTIES(luapb PROPERTIES PREFIX "")
    TARGET_LINK_LIBRARIES(luapb lua53 dmyaml libprotobuf libprotoc)

    IF (USE_LUAPB_TEST)
        ADD_EXECUTABLE(luapbtest ${LUAPBTEST_SOURCES})
        TARGET_LINK_LIBRARIES(luapbtest luapb lua53 libprotobuf libprotoc)
    ENDIF(USE_LUAPB_TEST)
ELSEIF (UNIX)
    ADD_DEFINITIONS(-DLUA_USE_LINUX)

    IF (USE_LUAPB_INNER)
        ADD_LIBRARY(lua53 SHARED ${DMLUA_SOURCES})
        ADD_EXECUTABLE(lua ${LUA_SOURCES})
        TARGET_LINK_LIBRARIES(lua lua53 m dl)
        SET_TARGET_PROPERTIES(lua53 PROPERTIES COMPILE_FLAGS "-Wl,-E")
    ENDIF(USE_LUAPB_INNER)

    ADD_LIBRARY(luapb SHARED ${LUAPB_SOURCES})
    SET_TARGET_PROPERTIES(luapb PROPERTIES COMPILE_FLAGS "-Wl,-E" )
    SET_TARGET_PROPERTIES(luapb PROPERTIES PREFIX "")
    TARGET_LINK_LIBRARIES(luapb lua53 dmyaml libprotobuf libprotoc)

    IF (USE_LUAPB_TEST)
        ADD_EXECUTABLE(luapbtest ${LUAPBTEST_SOURCES})
        TARGET_LINK_LIBRARIES(luapbtest luapb lua53 libprotobuf libprotoc)
    ENDIF(USE_LUAPB_TEST)
ENDIF ()

#ADD_CUSTOM_COMMAND(
#TARGET luapbtest
#PRE_BUILD
#COMMAND ${CMAKE_COMMAND} -E copy_if_different
#${CMAKE_CURRENT_SOURCE_DIR}/config/test.csv
#$<TARGET_FILE_DIR:luapbtest>
#)

#INSTALL(DIRECTORY include/ DESTINATION include)
#INSTALL(TARGETS luapb
#        RUNTIME DESTINATION bin
#        LIBRARY DESTINATION lib
#        ARCHIVE DESTINATION lib)

#SET(LUAPB_LIBS "luapb")
#MARK_AS_ADVANCED(${LUAPB_LIBS})
